package main.java.房屋租借系统.Serives;

import main.java.房屋租借系统.JavaBeen.House;
import main.java.房屋租借系统.JavaBeen.User;
import main.java.房屋租借系统.tool.HouseOppen;

import java.util.List;
import java.util.Scanner;

/**
 * @Auther:Liu
 * @Testname:vIEWS
 * @Date:2025/3/20 12:38
 */
public class Serives {
    public void Mains() throws Exception {
        boolean falge = true;
        Scanner scanner = new Scanner(System.in);
        HouseOppen houseOppen = new HouseOppen();
        System.out.println("-------------登录系统--------------");
        System.out.println("请输入username:");
        String admire = scanner.next();
        System.out.println("请输入password:");
        String password = scanner.next();
        User search = houseOppen.load(admire, password);
        String str = houseOppen.Ma();
        System.out.println("验证码如下:" + str);
        System.out.println("请输入验证码:");
        String str1 = houseOppen.readString(5);
        if (search != null && str.equals(str1)) {
            System.out.println("登陆成功");
            int key = 0;
            do {
                scanner = new Scanner(System.in);
                System.out.println("-------------房屋出租系统--------------");
                System.out.println("1  新  增  房  源");
                System.out.println("2  查  找  房  屋");
                System.out.println("3  删  除  房  屋");
                System.out.println("4  修  改  房  屋  信  息");
                System.out.println("5  房  屋  列  表");
                System.out.println("6  退          出");
                System.out.println("请输入你的选择（1~6）:");
                key = HouseOppen.readInt(1);
                switch (key) {
                    case 1:
                        System.out.println("-------------添加房屋--------------");
                        System.out.println("请输入要插入的房屋信息:");
                        System.out.println("姓名:");
                        String Name = HouseOppen.readString(5);
                        System.out.println("电话:");
                        int Phone = HouseOppen.readInt(5);
                        System.out.println("住址:");
                        String Address = HouseOppen.readString(5);
                        int Rent = 0;
                        System.out.println("月租:");
                        Rent = HouseOppen.readInt(5);
                        System.out.println("状态（已出租/未出租）:");
                        String State = HouseOppen.readString(3);
                        House house = new House(null, Name, Phone, Address, Rent, State);
                        int insert = houseOppen.insert(house);
                        if (insert == 1) {
                            System.out.println("插入成功!");
                        } else {
                            System.out.println("插入失败!,对象信息已存在!");
                        }
                        break;
                    case 2:
                        System.out.println("-------------查找房屋--------------");
                        System.out.println("请输入要查找的房屋信息的Id:");
                        int Id = HouseOppen.readInt(1);
                        House seleteone = houseOppen.seleteone(Id);
                        if (seleteone != null) {
                            System.out.println("房屋信息如下:");
                            System.out.println(seleteone);
                        } else {
                            System.out.println("对象不存在!");
                        }
                        break;
                    case 3:
                        System.out.println("-------------删除房屋--------------");
                        System.out.println("请输入您要删除的房屋信息的Id:");
                        int Id1 = HouseOppen.readInt(1);
                        int delete = houseOppen.delete(Id1);
                        if (delete == 1) {
                            System.out.println("删除成功!");
                        } else {
                            System.out.println("删除失败!，对象不存在!");
                        }
                        break;
                    case 4:
                        System.out.println("-------------修改房屋--------------");
                        System.out.println("请输入要修改的房屋信息Id:");
                        int ID3 = HouseOppen.readInt(1);
                        House house2 = houseOppen.seleteone(ID3);
                        if (house2 == null) {
                            System.out.println("对象不存在!");
                            break;
                        }
                        System.out.println("请输入要修改的房屋信息:");
                        scanner.nextLine();
                        System.out.println("姓名:" + "[" + house2.getName() + "]");
                        String Name1 = HouseOppen.readString(5, house2.getName());
                        if (Name1.equals(house2.getName())) {
                            System.out.println("没有修改！");
                        } else {
                            house2.setName(Name1);
                        }
                        System.out.println("电话:" + "[" + house2.getPhone() + "]");
                        int Phone1 = HouseOppen.readInt(house2.getPhone());
                        house2.setPhone(Phone1);
                        System.out.println("住址:" + "[" + house2.getAddress() + "]");
                        String Address1 = HouseOppen.readString(5, house2.getAddress());
                        if (Address1.equals(house2.getAddress())) {
                            System.out.println("没有修改！");
                            System.out.println("--------");
                        } else {
                            house2.setAddress(Address1);
                        }
                        System.out.println("月租:" + "[" + house2.getRent() + "]");
                        int Rent1 = HouseOppen.readInt(house2.getRent());
                        house2.setRent((Rent1));
                        System.out.println("状态（已出租/未出租）:" + "[" + house2.getState() + "]");
                        String State1 = HouseOppen.readString(3, house2.getState());
                        if (State1.equals(house2.getState())) {
                            System.out.println("没有修改！");
                            System.out.println("--------");
                        } else {
                            house2.setState(State1);
                        }
                        int update = houseOppen.update(house2);
                        if (update == 1) {
                            System.out.println("修改成功!");
                        } else {
                            System.out.println("修改失败!,对象信息不存在!");
                        }
                        break;
                    case 5:
                        System.out.println("-------------房屋列表--------------");
                        List<House> houses = houseOppen.selectAll();
                        System.out.println("编号" + "    " + "房主" + "    " + "电话" + "    " + "地址" + "    " + "月租" + "     " + "出租状态(已出租/未出租)");
                        for (int i = 0; i < houses.size(); i++) {
                            System.out.println(houses.get(i).getId() + "      " + houses.get(i).getName() + "     " + houses.get(i).getPhone() + "    " + houses.get(i).getAddress() + "   " + houses.get(i).getRent() + "    " + houses.get(i).getState());
                        }
                        break;
                    case 6:
                        falge = false;
                        break;
                }
            } while (falge);
        } else {
            System.out.println("登录失败");
            houseOppen.broke();
        }
    }
}
